package com.third.clock.ui.model.presenter;

import android.content.Context;
import android.text.TextUtils;

import com.third.clock.bean.LoginEntity;
import com.third.clock.bean.ReportEntity;
import com.third.clock.bean.ResponseData;
import com.third.clock.common.Contants;
import com.third.clock.manager.ClockManager;
import com.third.clock.manager.OkHttpClientManager;
import com.third.clock.manager.TimerManager;
import com.third.clock.manager.TransformManager;
import com.third.clock.ui.model.IMindsFragmentView;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2018/7/23 0023.
 */

public class MindsFragmentPresenter {

    private Context mContext;

    private IMindsFragmentView mView;

    private OkHttpClientManager mOkHttpClientManager;

    private ClockManager clockManager;

    private int pageindex=0;

    private int pagesize=10;

    public enum LoadType{
        LOADMORE,REFRESH;
    }

    public MindsFragmentPresenter(Context context){
        this.mContext=context;
        clockManager=ClockManager.getInstace(mContext);
        mOkHttpClientManager=clockManager.getOkHttpClientManager();
    }

    public void setIMindsFragmentView(IMindsFragmentView view){
        this.mView=view;
    }

    public void onRefresh(final LoadType type){
        ResponseData<LoginEntity> responseData=clockManager.mLoginResponseData;
        if(responseData==null){
            return;
        }
        if(responseData.getData()==null){
            return;
        }
        if(mView!=null&&type==LoadType.REFRESH) mView.startRefreshing();
        Map<String,Object> getreportMap=new HashMap<>();
        getreportMap.put("userid",responseData.getData().getUser_id());
        if(type==LoadType.REFRESH){
            pageindex=1;
        }else{
            ++pageindex;
        }
        getreportMap.put("pageindex", pageindex);
        getreportMap.put("pagesize",pagesize);

        String toSign= TransformManager.sortMapByKey(getreportMap);
        getreportMap.put("sign",toSign);

        mOkHttpClientManager.post(Contants.REPORT_LOAD_URL,getreportMap, new OkHttpClientManager.okHttpCallback() {
            @Override
            public void onFailure(com.lzy.okgo.model.Response<String> s) {
                if (mView != null) {
                    if(type==LoadType.REFRESH) {
                        mView.stopRefreshing();
                    }else{
                        mView.onLoadmorestop();
                    }
                }
            }

            @Override
            public void onResponse(com.lzy.okgo.model.Response<String> response) {
                if(type==LoadType.REFRESH) {
                    mView.stopRefreshing();
                }else{
                    mView.onLoadmorestop();
                }
                if(response!=null&&response.body()!=null){
                    ResponseData<List<ReportEntity>> responseData=TransformManager.paserListReportEntity(response.body());
                    if(responseData!=null&&responseData.getCode()==Contants.CODE_0){
                        List<ReportEntity> list=responseData.getData();
                        if (mView != null) mView.onRefreshData(type,list);
                    }
                }
            }
        });
    }

    public void postReport(String title,String conent){
       if(TextUtils.isEmpty(title)){
           if(mView!=null) mView.onReportFail("标题不能为空");
           return;
       }else if(TextUtils.isEmpty(conent)){
           if(mView!=null) mView.onReportFail("内容不能为空");
           return;
       }

       if(clockManager!=null&&mOkHttpClientManager!=null){
           ResponseData<LoginEntity> responseData=clockManager.mLoginResponseData;
           if(responseData!=null&&responseData.getData()!=null){
               Map<String,Object> reportMap=new HashMap<>();
               reportMap.put("userid",responseData.getData().getUser_id());
               try {
                   reportMap.put("title", title);
                   reportMap.put("content",conent);
               }catch (Exception e){

               }

               String toSign= TransformManager.sortMapByKey(reportMap);
               reportMap.put("sign",toSign);

               mOkHttpClientManager.post(Contants.REPORT_SAVE_URL, reportMap, new OkHttpClientManager.okHttpCallback() {
                   @Override
                   public void onFailure(com.lzy.okgo.model.Response<String> s) {
                       if(mView!=null) mView.onReportFail("提交失败");
                   }

                   @Override
                   public void onResponse(com.lzy.okgo.model.Response<String> response) {
                       if(response!=null&&response.body()!=null){
                           ResponseData<String> responseData=TransformManager.paserString(response.body()) ;
                           if(responseData!=null&&responseData.getCode()==Contants.CODE_0){
                               if(mView!=null) mView.onReportSuccess();
                           }else{
                               if(mView!=null) mView.onReportFail("提交失败");
                           }
                       }else{
                           if(mView!=null) mView.onReportFail("提交失败");
                       }
                   }
               });
           }
       }

    }

    public void removeReport(final int listpostion,ReportEntity mReportEntity){
       if(mReportEntity==null){
           if(mView!=null)mView.onReportFail("删除失败");
           return;
       }
        ResponseData<LoginEntity> responseData=clockManager.mLoginResponseData;
        if(responseData==null){
            if(mView!=null)mView.onReportFail("删除失败");
            return;
        }
        if(responseData.getData()==null){
            if(mView!=null)mView.onReportFail("删除失败");
            return;
        }

        Map<String,Object> removeMap=new HashMap<>();
        removeMap.put("userid",responseData.getData().getUser_id());
        removeMap.put("ids",mReportEntity.getId());

        String toSign=TransformManager.sortMapByKey(removeMap);
        removeMap.put("sign",toSign);

        mOkHttpClientManager.post(Contants.REPORT_DELECT_URL, removeMap, new OkHttpClientManager.okHttpCallback() {
            @Override
            public void onFailure(com.lzy.okgo.model.Response<String> s) {
                if(mView!=null)mView.onReportFail("删除失败");
            }

            @Override
            public void onResponse(com.lzy.okgo.model.Response<String> response) {
                 if(response!=null&&response.body()!=null){
                     ResponseData<String> responseData=TransformManager.paserString(response.body());
                     if(responseData!=null&&responseData.getCode()==Contants.CODE_0){
                         if(mView!=null)mView.onRemoveSuccess(listpostion);
                     }else{
                         if(mView!=null)mView.onReportFail("删除失败");
                     }
                 }else{
                     if(mView!=null)mView.onReportFail("删除失败");
                 }
            }
        });

    }


}
